﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ECP.Utilities.Controls
{
    public partial class UCTableSort : DevExpress.XtraEditors.XtraUserControl
    {
        private ECP.Utilities.Controls.ECPGridView dataGridView;
        private DataView dataView;
        private bool PermissionEdit = true;
        private BaseUserInfo userInfo = new BaseUserInfo();

        public delegate string GetSequence(BaseUserInfo UserInfo, string TableName);
        public delegate string GetDegression(BaseUserInfo UserInfo, string TableName);

        public event GetSequence GetSequenceEvent;

        public event GetDegression GetDegressionEvent;


        public UCTableSort()
        {
            InitializeComponent();
          
        }

        public void SetService(GetSequence ent, GetDegression desc)
        {

            GetSequenceEvent += ent;
            GetDegressionEvent += desc;
        }

        public void DataBind(BaseUserInfo userInfo, ECP.Utilities.Controls.ECPGridView dataGridView)
        {
            this.DataBind(userInfo, dataGridView, true);
        }

        public void DataBind(BaseUserInfo userInfo, ECP.Utilities.Controls.ECPGridView dataGridView, bool permissionEdit)
        {
            this.PermissionEdit = permissionEdit;
            this.UserInfo = userInfo;
            this.dataGridView = dataGridView;

            if ((dataGridView.DataSource != null))
            {
                DataTable dt = dataGridView.DataSource as DataTable;
                if (dt != null)
                {
                    this.dataView = dt.DefaultView;

                }
                else
                {
                    this.dataView = (DataView)dataGridView.DataSource;
                }
               
            }
            this.SetControlState();
        }



        public int SetBottom()
        {
            if (BaseSortLogic.GetNextID(this.dataView, this.EntityID).Length > 0)
            {
                //   string sequence = SequenceService.Instance.GetSequence(this.UserInfo, this.dataView.Table.TableName);
                if (GetSequenceEvent == null)
                {
                    return 0;
                }
                string sequence = GetSequenceEvent(this.UserInfo, this.dataView.Table.TableName);

                return BaseBusinessLogic.SetProperty(this.dataView.Table, this.EntityID, BaseBusinessLogic.FieldSortCode, sequence);
            }
            if (BaseSystemInfo.ShowInformation)
            {
                XtraMessageBox.Show(AppMessage.MSG0022, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return 0;
        }

        public void SetControlState()
        {
            this.SetSortButton(false);
            if ((this.dataView != null) && (this.dataView.Count > 1))
            {
                this.SetSortButton(this.PermissionEdit);
            }
        }

        public int SetDown()
        {
            string nextID = BaseSortLogic.GetNextID(this.dataView, this.EntityID);
            if (nextID.Length > 0)
            {
                return BaseSortLogic.Swap(this.dataView.Table, this.EntityID, nextID);
            }
            if (BaseSystemInfo.ShowInformation)
            {
                XtraMessageBox.Show(AppMessage.MSG0022, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return 0;
        }

        public void SetSortButton(bool enabled)
        {
            this.btnSetTop.Enabled = enabled;
            this.btnSetUp.Enabled = enabled;
            this.btnSetDown.Enabled = enabled;
            this.btnSetBottom.Enabled = enabled;
        }

        public int SetTop()
        {
            if (BaseSortLogic.GetPreviousID(this.dataView, this.EntityID).Length > 0)
            {
                //  string degression = SequenceService.Instance.GetDegression(this.UserInfo, this.dataView.Table.TableName);
                if (GetDegressionEvent == null)
                {
                    return 0;
                }
                string degression = GetDegressionEvent(this.UserInfo, this.dataView.Table.TableName);
                return BaseBusinessLogic.SetProperty(this.dataView.Table, this.EntityID, BaseBusinessLogic.FieldSortCode, degression);
            }
            if (BaseSystemInfo.ShowInformation)
            {
                XtraMessageBox.Show(AppMessage.MSG0021, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return 0;
        }

        public int SetUp()
        {
            string previousID = BaseSortLogic.GetPreviousID(this.dataView, this.EntityID);
            if (previousID.Length > 0)
            {
                return BaseSortLogic.Swap(this.dataView.Table, this.EntityID, previousID);
            }
            if (BaseSystemInfo.ShowInformation)
            {
                XtraMessageBox.Show(AppMessage.MSG0021, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return 0;
        }


        public string EntityID
        {
            get
            {
                return BaseInterfaceLogic.GetDataGridViewEntityID(this.dataGridView, BaseBusinessLogic.FieldID);
            }
        }

        public bool SetBottomEnabled
        {
            get
            {
                return this.btnSetBottom.Enabled;
            }
            set
            {
                this.btnSetBottom.Enabled = value;
            }
        }

        public bool SetDownEnabled
        {
            get
            {
                return this.btnSetDown.Enabled;
            }
            set
            {
                this.btnSetDown.Enabled = value;
            }
        }

        public bool SetTopEnabled
        {
            get
            {
                return this.btnSetTop.Enabled;
            }
            set
            {
                this.btnSetTop.Enabled = value;
            }
        }

        public bool SetUpEnabled
        {
            get
            {
                return this.btnSetUp.Enabled;
            }
            set
            {
                this.btnSetUp.Enabled = value;
            }
        }

        public BaseUserInfo UserInfo
        {
            get
            {
                this.userInfo = new BaseUserInfo();
                if (!DesignMode)
                {
                    this.userInfo.GetUserInfo();
                }
                return this.userInfo;
            }
            set
            {
                this.userInfo = value;
            }
        }

        private void UCTableSort_Load(object sender, EventArgs e)
        {
            this.SetSortButton(false);
        }
        private void btnSetBottom_Click(object sender, EventArgs e)
        {
            this.SetBottom();
        }

        private void btnSetDown_Click(object sender, EventArgs e)
        {
            this.SetDown();
        }

        private void btnSetTop_Click(object sender, EventArgs e)
        {
            this.SetTop();
        }

        private void btnSetUp_Click(object sender, EventArgs e)
        {
            this.SetUp();
        }
    }
}
